//
//  DateFormatter+AG.swift
//  BWExtensionKit
//
//  Created by zhuxuhong on 2022/2/21.
//

import Foundation

extension DateFormatter {
    public enum DateJoinTag {
        case line1, line2, dot1, dot2, slash1, slash2, comma
        case individual(year: String, month: String, day: String)
        var value: [String] {
            switch self {
            case .line1: return ["-"]
            case .line2: return ["_"]
            case .dot1: return ["."]
            case .dot2: return ["·"]
            case .slash1: return ["/"]
            case .slash2: return ["\\"]
            case .comma: return [","]
            case .individual(let year, let month, let day):
                return [
                    year, month, day
                ]
            }
        }
    }
    public enum TimeJoinTag {
        case colon
        case individual(hour: String, minute: String, second: String, milSecond: String)
        var value: [String] {
            switch self {
            case .colon:
                return [":"]
            case .individual(let hour, let minute, let second, let milSecond):
                return [
                    hour, minute, second, milSecond
                ]
            }
        }
    }
}

extension DateFormatter.DateJoinTag {
    public static let cn1: Self = .individual(year: "年", month: "月", day: "日")
    public static let cn2: Self = .individual(year: "年", month: "月", day: "号")
}
extension DateFormatter.TimeJoinTag {
    public static let cn1: Self = .individual(hour: "时", minute: "分", second: "秒", milSecond: "毫秒")
    public static let cn2: Self = .individual(hour: "点", minute: "分", second: "秒", milSecond: "毫秒")
}

extension DateFormatter {
    static let shared = DateFormatter()
    
    public static func bw_dateFormatString(
        components: Set<Calendar.Component>,
        joinTag: DateJoinTag = .line1
    ) -> String {
        var format = ""
        if components.contains(.year) {
            format += "yyyy@Y"
        }
        if components.contains(.month) {
            format += "MM@M"
        }
        if components.contains(.day) {
            format += "dd@D"
        }
        switch joinTag {
        case .individual(let year, let month, let day):
            format = format.bw_replaces(of: "@Y","@M","@D", with: year,month,day)
        case .line1, .line2, .dot1, .dot2, .slash1, .slash2, .comma:
            let tag = joinTag.value.first!
            format = format.replacingOccurrences(of: "@D", with: "")
            format = format.bw_replaces(of: "@Y","@M", by: tag)
        }
        return format
    }
    
    public static func bw_timeFormatString(
        components: Set<Calendar.Component>,
        joinTag: TimeJoinTag = .colon
    ) -> String {
        var format = ""
        if components.contains(.hour) {
            format += "HH@H"
        }
        if components.contains(.minute) {
            format += "mm@M"
        }
        if components.contains(.second) {
            format += "ss@s"
        }
        if components.contains(.nanosecond) {
            format += "SSS@S"
        }
        switch joinTag {
        case .colon:
            let tag = joinTag.value.first!
            format = format.replacingOccurrences(of: "@S", with: "")
            format = format.bw_replaces(of: "@H","@M","@s","@S", by: tag)
        case .individual(let hour, let minute, let second, let milSecond):
            format = format.bw_replaces(of: "@H","@M","@s","@S", with: hour,minute,second,milSecond)
        }
        return format
    }
}

